package com.ktgj.sms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.sms.domain.SMSInfo;
import com.ktgj.sms.domain.vo.SmsContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.sms.mapper.SmsAirlineticketMapper;
import com.ktgj.sms.domain.SmsAirlineticket;
import com.ktgj.sms.service.ISmsAirlineticketService;

import javax.servlet.http.HttpServletRequest;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-04
 */
@Service
public class SmsAirlineticketServiceImpl implements ISmsAirlineticketService 
{
    @Autowired
    private SmsAirlineticketMapper smsAirlineticketMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param keyId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public SmsAirlineticket selectSmsAirlineticketById(Long keyId)
    {
        return smsAirlineticketMapper.selectSmsAirlineticketById(keyId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param smsAirlineticket 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SmsAirlineticket> selectSmsAirlineticketList(SmsAirlineticket smsAirlineticket)
    {
        return smsAirlineticketMapper.selectSmsAirlineticketList(smsAirlineticket);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param smsAirlineticket 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSmsAirlineticket(SmsAirlineticket smsAirlineticket)
    {
        smsAirlineticket.setCreateTime(DateUtils.getNowDate());
        return smsAirlineticketMapper.insertSmsAirlineticket(smsAirlineticket);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param smsAirlineticket 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSmsAirlineticket(SmsAirlineticket smsAirlineticket)
    {
        return smsAirlineticketMapper.updateSmsAirlineticket(smsAirlineticket);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param keyIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSmsAirlineticketByIds(Long[] keyIds)
    {
        return smsAirlineticketMapper.deleteSmsAirlineticketByIds(keyIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param keyId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteSmsAirlineticketById(Long keyId)
    {
        return smsAirlineticketMapper.deleteSmsAirlineticketById(keyId);
    }

    /**
     * 短信猫
     * */
    @Override
    public int smsCat(SMSInfo smsInfo) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        SmsAirlineticket smsAirlineticket = new SmsAirlineticket();
        Long id = System.currentTimeMillis() / 1000;
        smsAirlineticket.setKeyId(id);
        smsAirlineticket.setSmsId(smsInfo.id);
        smsAirlineticket.setSendMoblie(smsInfo.sendMoblie);
        smsAirlineticket.setSmsContent(smsInfo.smsContent);

        if (smsInfo.receiveTime != null && !smsInfo.receiveTime.equals("")){
            try {
                smsAirlineticket.setReceiveTime(format.parse(smsInfo.receiveTime));
            } catch (Exception e){
                throw new Exception("时间转换错误");
            }
        }

        smsAirlineticket.setState("1");
//        smsAirlineticket.setSmsIdKey(Long.valueOf(smsInfo.smsId));

//        /** 设备号 */
//        @Excel(name = "设备号")
//        private String imei;
        smsAirlineticket.setReceiveMoblie(smsInfo.receiveMoblie);
        int i = smsAirlineticketMapper.insertSmsAirlineticket(smsAirlineticket);
        return i;
    }

    /**
     * 短信内容查询
     * */
    @Override
    public List<SmsAirlineticket> smsContent(SmsContentVo smsContentVo) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (smsContentVo.getEmployee() != null && !smsContentVo.getEmployee().equals("")){
            Map<String, Object> map = new HashMap<>();
            map.put("name", smsContentVo.getEmployee());
            List<Long> longList = remoteCustomerService.employeeIdList(map);
            smsContentVo.setEmployeeIdList(longList);
        }

        List<SmsAirlineticket> smsAirlinetickets = smsAirlineticketMapper.smsContent(smsContentVo);
        if (smsAirlinetickets != null && smsAirlinetickets.size() > 0){
            for (SmsAirlineticket smsAirlineticket : smsAirlinetickets) {
                if (smsAirlineticket.getOperationemployee() != null){
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", smsAirlineticket.getOperationemployee());
                    Employee employee = remoteCustomerService.employeeNameList(map);
                    if (employee != null){
                        smsAirlineticket.setEmployee(employee.getEmployeeName());
                    }
                }

                if (smsAirlineticket.getOperationtime() != null){
                    smsAirlineticket.setOperationDate(format.format(smsAirlineticket.getOperationtime()));
                }
            }
        }
        return smsAirlinetickets;
    }

    /**
     * 短信内容详情
     * */
    @Override
    public SmsAirlineticket smsContentDetails(HttpServletRequest request, Long id) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SmsAirlineticket smsAirlineticket = smsAirlineticketMapper.smsContentDetails(id);
        String token = request.getHeader("token");
        Employee employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();
        if (employee != null){
            smsAirlineticket.setOperationemployee(employee.getEmployeeId());
            smsAirlineticket.setEmployee(employee.getEmployeeName());
        }
        smsAirlineticket.setOperationDate(format.format(new Date()));
        smsAirlineticket.setOperationtime(new Date());
        return smsAirlineticket;
    }

    /**
     * 短信内容处理
     * */
    @Override
    public int smsContentAudit(SmsAirlineticket smsAirlineticket) {
        smsAirlineticket.setState("2");
        int i = smsAirlineticketMapper.updateSmsAirlineticket(smsAirlineticket);
        return i;
    }
}
